<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Course\Video\CreateRequest;
use App\Http\Requests\Course\Video\UpdateRequest;
use App\Models\Category;
use App\Models\Qiniu;
use App\Models\Teacher;
use App\Models\Video;
use App\Traits\WithFile;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class VideoController extends Controller
{
    use WithFile;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('课程管理')) {
            return view('error.error');
        }
        $videos = Video::with(['category', 'teacher', 'imaging'])
            ->where(function ($query) {
                if ($title = \request()->get('title', '')) {
                    $query->where('title', 'like', '%' . $title . '%');
                }
                if ($category_id = \request()->get('category_id', '')) {
                    $query->where('category_id', $category_id);
                }
                if ($teacher_id = \request()->get('teacher_id', '')) {
                    $query->where('teacher_id', $teacher_id);
                }
            })
            ->orderBy('created_at', 'desc')
            ->paginate(15);
        $category = Category::all();
        $teacher = Teacher::all();
        return view('video.list', [
            'videos'    => $videos,
            'categorys' => $category,
            'teachers'  => $teacher
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('课程管理')) {
            return view('error.error');
        }
        return view('video.create', [
            'categorys' => Category::all(),
            'teachers'  => Teacher::all()
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param CreateRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Throwable
     */
    public function store(CreateRequest $request)
    {
        $model = \DB::transaction(function () use ($request) {
            $keys = array_keys($request->validated());
            $video = new Video();
            $video = $this->setModel($video)->upload($keys, $request, 'public');
            return $video;
        });
        return redirect()->route('video.index')->with('success', '添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('课程管理')) {
            return view('error.error');
        }
        $video = Video::with(['imaging', 'videoing'])->find($id);
        return view('video.create', [
            'video'     => $video,
            'categorys' => Category::all(),
            'teachers'  => Teacher::all()
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UpdateRequest $request
     * @param Video $video
     * @return \Illuminate\Http\Response
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, Video $video)
    {

        $model = \DB::transaction(function () use ($request, $video) {
            $keys = array_keys($request->validated());
            $video = $this->setModel($video)->upload($keys, $request, 'public');
            return $video;
        });
        return redirect()->route('video.index')->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
